<?php
##############################################
#
#   评论管理
# @ Time : 2017-1-3 - 下午10:45:24
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################

namespace Admin\Controller;
use Admin\Model\AdminBaseModel; 
use Admin\Model\CommentModel;



class CommentController extends AdminBaseController {
	
	protected $_comment;
	
	public function _initialize(){
		parent::_initialize();
		$this->_comment  = new CommentModel();
	}
	
	/**
	 * 评论列表
	 */
	public function commentList(){
	   /*  $data = $this->_comment->getComment(I('activity_name'), I('type'), I('category'));
	    $this -> assign('comment', $data[0]);
	    $this -> assign('page', $data[1]); */
	    $this -> display('index');
	}
	
	public function ajaxindex(){
	    $model = M('comment');
	    $username = I('user_name','','trim');
	    $status = I('status');
	    if($status){
	        $where['status'] = $status;
	    }
	    $where['parent_id'] = 0;
	    if($username){
	        $where['user_name'] = array('like','%'.$user_name.'%');
	    }
	    $where['_logic'] = 'AND';
	    $count = $model->where($where)->count();
	    $Page  = new \Think\AjaxPage($count,C('listRows'));
	    $show = $Page->show();
	    $comment_list = $model->where($where)->order('add_time DESC')->limit($Page->firstRow.','.$Page->listRows)->select();
//            var_dump($comment_list);
	    $this->assign('comment',$comment_list);
	    $this->assign('page',$show);// 赋值分页输出
	    $this->display('ajaxindex');
	}
	
	/**
	 * 评论详情
	 */
 	public function detail(){
 	    $id = I('get.id');
 	    $res = M('comment')->where(array('id'=>$id))->find();
 	    if(!$res){
 	        exit($this->error('不存在该评论'));
 	    }
 	    if(IS_POST){
 	        $data['parent_id'] = $id;
 	        $data['comment_type'] = $res['comment_type'];
 	        $data['email'] = 'admin@qq.com';
 	        $data['user_id'] = 110;
 	        $data['ip_address'] = '127.0.0.1';
 	        $data['content'] = I('post.content');
 	        $data['is_value'] = $res['id_value'];
 	        $data['add_time'] = time();
 	        $data['user_name'] = 'admin';
 	        $data['status'] = 1;
 	        $row =  M('comment')->add($data);
 	        if($row){
 	            $this->success('添加成功');
 	        }else{
 	            $this->error('添加失败');
 	        }
 	        exit;
 	    }
 	    $reply = M('comment')->where(array('parent_id'=>$id))->select(); // 评论回复列表
 	     
 	    $this->assign('comment',$res);
 	    $this->assign('reply',$reply);
		$this->display('detail');
	}

    /**
     * 评论详情
     */
    public function make_detail(){
        if(IS_POST){
            $data = I('post.');
            $arr = array(
                'content'=>$data['content'],
                'status'=>$data['status'],
            );
            $result = M('comment')->where('id='.$data['id'])->save($arr);
            if($result){
                $this->success('修改成功',U('commentList'),1);
            }else{
                $this->error('操作失败',1);
            }
        }else{
            $id = I('id');
            $comment = M('comment')->alias('c')->field('c.*,a.headimgurl')->join('__APPLY__ as a on a.user_id=c.user_id')->where('c.id='.$id)->find();
            $this->assign('comment',$comment);
            $this->display();
        }

    }

	/**
	 * 全选删除活动
	 */
    public function deleteAllActivity(){
	    if(!IS_POST) return;
	    $result = $this->_activity->deleteAllActivity($_POST);
	    if($result)
			$this -> success('删除成功', U('activityList'), 2);
		else
			$this -> error('操作失败', '', 1);
	}
	
	/**
	 * 操作
	 */
	public function operate(){
	    $operate = I('post.operate');
	    $selected_id_arr = I('post.comment_id');
	    if(!in_array($operate,array('del','show','hide')) || !is_array($selected_id_arr)){
	        $this->error('非法操作');
	    }
	    $map['id'] = array('in', $selected_id_arr);
	    $comment = M('comment');
	    switch ($operate){
	        case 'del':
	            $map['parent_id'] = array('in', $selected_id_arr);
	            $map['_logic'] = 'OR';
	            $row = $comment->where($map)->delete();
	            break;
	        case 'show':
	            $data = array('status'=>1);
	            $row = $comment->where($map)->save($data);
	            break;
	        case 'hide':
	            $data = array('status'=>0);
	            $row = $comment->where($map)->save($data);
	            break;
	    }
	    if($row !== false)
	        $this->success('操作成功', U('commentList'), 1);
	    else
	        $this->error('操作失败');
	        
	}
	
	/**
	 * 删除评论
	 */
	public function deleteComment(){
		if(!IS_AJAX) return;
		$result = $this->_comment->deleteComment(intval($_GET['id']));
		if($result)
			$this->ajaxReturn(array('status' => 1,'message' => '删除成功'));
		else
			$this->ajaxReturn(array('status' => 2,'message' => '操作失败'));
	}
	
	/**
	 * 分类列表
	 */
	public function categoryList() {
	    $data = $this->_activity->getCategory(I('name'));
	    $this->assign('category', $data[0]);
	    $this->assign('page', $data[1]);
	    $this->display('category_index');
	}

	/**
	 * 添加分类
	 */
	public function addCategory() {
	    if(IS_GET){
	        $id = I('id');
	        $act = I('get.act');
	        if($id && $act == 'edit'){
	            $data = $this->_activity->getCategoryFind($id);
	            $data['describe'] = htmlspecialchars_decode($data['describe']);
	            $this->assign('id', $id);
	            $this->assign('category', $data);
	        }
	        $this->assign('act', $act);
	        $this->display('add_category');
	    }else if(IS_POST){
	        $data = I('post.');
	        $data['add_time'] = time();
	        $admin_base_model = new AdminBaseModel('activity_category');
	        $act = I('post.act');
	        if($act == 'add'){
	            $result = $admin_base_model->addData($data);
	            if($result)
	                $this->success('添加成功', U('categoryList'), 1);
	            else
	                $this->error('操作失败', '', 1);
	        }elseif($act == 'edit'){
	            $id = I('id');
	            if($this->_activity->editCategory($id, $data) !== false)
	                $this->success('修改成功', U('categoryList'), 1);
	            else
	                $this->error('操作失败', '', 1);
	        }
	    }
	}
	
	/**
	 * 删除分类
	 */
	public function deleteCategory() {
	    if(!IS_AJAX) return;
		$result = $this->_activity->deleteCategory(intval($_GET['id']));
		if($result)
			$this->ajaxReturn(array('status' => 1,'message' => '删除成功'));
		else
			$this->ajaxReturn(array('status' => 2,'message' => '操作失败'));
	}
	
	/**
	 * 全选删除分类
	 */
	public function deleteAllCategory(){
	    if(!IS_POST) return;
	    $result = $this->_activity->deleteAllCategory($_POST);
	    if($result)
	        $this->success('删除成功', U('categoryList'), 1);
	    else
	        $this->error('操作失败', '', 1);
	}
}



	
	